package org.dromara.system.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.XcxFollowVo;
import org.dromara.system.domain.bo.XcxFollowBo;
import org.dromara.system.service.IXcxFollowService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 我的关注
 *
 * @author Lion Li
 * @date 2024-05-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/follow")
public class XcxFollowController extends BaseController {

    private final IXcxFollowService xcxFollowService;

    /**
     * 查询我的关注列表
     */
    @SaCheckPermission("system:follow:list")
    @GetMapping("/list")
    public TableDataInfo<XcxFollowVo> list(XcxFollowBo bo, PageQuery pageQuery) {
        return xcxFollowService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出我的关注列表
     */
    @SaCheckPermission("system:follow:export")
    @Log(title = "我的关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XcxFollowBo bo, HttpServletResponse response) {
        List<XcxFollowVo> list = xcxFollowService.queryList(bo);
        ExcelUtil.exportExcel(list, "我的关注", XcxFollowVo.class, response);
    }

    /**
     * 获取我的关注详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:follow:query")
    @GetMapping("/{id}")
    public R<XcxFollowVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(xcxFollowService.queryById(id));
    }

    /**
     * 新增我的关注
     */
    @SaCheckPermission("system:follow:add")
    @Log(title = "我的关注", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XcxFollowBo bo) {
        return toAjax(xcxFollowService.insertByBo(bo));
    }

    /**
     * 修改我的关注
     */
    @SaCheckPermission("system:follow:edit")
    @Log(title = "我的关注", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XcxFollowBo bo) {
        return toAjax(xcxFollowService.updateByBo(bo));
    }

    /**
     * 删除我的关注
     *
     * @param ids 主键串
     */
    @Log(title = "我的关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(xcxFollowService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 删除我的关注
     *
     */
    @Log(title = "我的关注", businessType = BusinessType.DELETE)
    @PostMapping("/deleteFollow")
    public R<Void> deleteFollow(@RequestBody  XcxFollowBo bo) {
        bo.setCreateBy(LoginHelper.getUserId());
        List<XcxFollowVo> followVoList = xcxFollowService.queryList(bo);
        if (followVoList.size() > 0){
            List<Long> idList = new ArrayList<>();
            for (XcxFollowVo xcxFollowVo:followVoList) {
                idList.add(xcxFollowVo.getId());
            }
            return toAjax(xcxFollowService.deleteWithValidByIds(idList, true));
        }

        return R.fail("未查询到相应的关注信息");
    }

}
